using System;
using System.Collections;
using System.Data;
using PCSComProduct.Items.DS;
using PCSComUtils.Common;

using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;
using PCSComUtils.DataContext;
using System.Collections.Generic;
using System.Transactions;
using PCSComUtils.DataAccess;
using System.Linq;

namespace PCSComProduct.Items.BO
{
    public class BomBO //: IBomBO
    {
        public BomBO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IObjectBO Members

        //**************************************************************************              
        ///    <Description>
        ///		Updata dataset contains Boms
        ///    </Description>
        ///    <Inputs>
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       void
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Wednesday, Mar 16, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        public void UpdateDataSet(DataSet dstData)
        {
            // TODO:  Add BomBO.UpdateDataSet implementation
            try
            {
                ITM_BOMDS dsBOM = new ITM_BOMDS();
                dsBOM.UpdateDataSet(dstData);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(object pObjectDetail)
        {
            // TODO:  Add BomBO.Update implementation
        }

        public void Delete(object pObjectVO)
        {
            // TODO:  Add BomBO.Delete implementation
        }

        public void Add(object pObjectDetail)
        {
            // TODO:  Add BomBO.Add implementation
        }

        public object GetObjectVO(int pintID, string VOclass)
        {
            // TODO:  Add BomBO.GetObjectVO implementation
            return null;
        }

        #endregion

        //**************************************************************************              
        ///    <Description>
        ///		Get product's informations by ProductID
        ///    </Description>
        ///    <Inputs>
        ///		ProductID
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       object
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Tuesday, Mar 21, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public object GetObjectVOForBOM(int pintID)
        {
            try
            {
                return new ITM_ProductDS().GetObjectVOForBOM(pintID);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///		Get UM object by ID
        ///    </Description>
        ///    <Inputs>
        ///		UMID
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       object
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Tuesday, Mar 21, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public object GetObjectUM(int pintUMID)
        {
            try
            {
                return new MST_UnitOfMeasureDS().GetObjectVO(pintUMID);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        //**************************************************************************              
        ///    <Description>
        ///    List routings of the product
        ///    </Description>
        ///    <Inputs>
        ///    ProductID
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       void
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Tuesday, Apr 26, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public DataTable ListRoutingOfProduct(int pintProductID)
        {
            try
            {
                return new ITM_RoutingDS().ListRoutingByProduct(pintProductID).Tables[0];
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///		Get product's informations by Code
        ///    </Description>
        ///    <Inputs>
        ///		Code
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       object
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Tuesday, Mar 21, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public object GetObjectVOForBOMByCode(string pstrCode)
        {
            try
            {
                return new ITM_ProductDS().GetObjectVOForBOMByCode(pstrCode);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///			Update all data after
        ///			 BOM details
        ///			 Hierarchy infomation
        ///    </Description>
        ///    <Inputs>
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       void
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Wednesday, Mar 16, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public void UpdateAll(DataSet dstData, object pobjProduct)
        {
            // TODO:  Add BomBO.UpdateDataSet implementation
            try
            {
                //update dataset BOM
                ITM_BOMDS dsBOM = new ITM_BOMDS();
                DataSet dstBOM = new DataSet();
                dstBOM.Tables.Add(dstData.Tables[ITM_BOMTable.TABLE_NAME].Copy());
                dsBOM.UpdateDataSet(dstBOM);

                //update dataset Hierarchy
                DataSet dstHierarchy = new DataSet();
                dstHierarchy.Tables.Add(dstData.Tables[ITM_HierarchyTable.TABLE_NAME].Copy());
                ITM_HierarchyDS dsHierarchyDS = new ITM_HierarchyDS();
                dsHierarchyDS.UpdateDataSet(dstData);

                //update Product
                new ITM_ProductDS().UpdateForBom(pobjProduct);
                dstData.AcceptChanges();
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdateBOM(List<ITM_BOM> listBOM, List<ITM_Hierarchy> listHierachy, ITM_Product pObjectDetail)
        {
            // TODO:  Add BomBO.UpdateDataSet implementation
            try
            {
                using (var trans = new TransactionScope())
                {
                    using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                    {
                        try
                        {
                            var listBOMDel = db.ITM_BOMs.Where(e => e.ProductID == pObjectDetail.ProductID);
                            //Remove BOM
                            if (listBOMDel.Count() > 0)
                            { db.ITM_BOMs.DeleteAllOnSubmit(listBOMDel); db.SubmitChanges(); }
                            var listHierachyDel = db.ITM_Hierarchies.Where(e => e.Source == pObjectDetail.ProductID);
                            // Remove Hierachy
                            if (listHierachyDel.Count() > 0)
                            {
                                db.ITM_Hierarchies.DeleteAllOnSubmit(listHierachyDel);
                                db.SubmitChanges();
                            }
                            //Insert into Data base
                            db.ITM_Hierarchies.InsertAllOnSubmit(listHierachy);
                            db.ITM_BOMs.InsertAllOnSubmit(listBOM);

                            var obj = db.ITM_Products.SingleOrDefault(e => e.ProductID == pObjectDetail.ProductID);

                            obj.BOMDescription = pObjectDetail.BOMDescription;
                            obj.BomIncrement = pObjectDetail.BomIncrement;
                            db.SubmitChanges();
                        }
                        catch
                        {
                            Transaction.Current.Rollback();
                        }
                        finally { trans.Complete(); }
                    }
                }
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///		Get all Bom's details of product by ProductID
        ///    </Description>
        ///    <Inputs>
        ///		ProductID
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       void
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Wednesday, Mar 16, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public DataTable ListBOMDetailsOfProduct(int pintProductID)
        {
            ITM_BOMDS dsBOM = new ITM_BOMDS();
            return dsBOM.ListBomDetailOfProduct(pintProductID);
        }

        //**************************************************************************              
        ///    <Description>
        ///		Get all hierarchy of product which constructor BOM
        ///    </Description>
        ///    <Inputs>
        ///		ProductID
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       void
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Wednesday, Mar 16, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public DataTable ListHierarchyOfProduct(int pintProductID)
        {
            try
            {
                return new ITM_HierarchyDS().ListForProduct(pintProductID);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        //**************************************************************************              
        ///    <Description>
        ///		Check rule of BOM for Product
        ///			Get listparent of Product
        ///			and then Check
        ///    </Description>
        ///    <Inputs>
        ///		ProductID
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       ArrayList : each object in ArrayList contains two informations are index of the wrong row and ID
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Wednesday, Mar 16, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public ArrayList CheckBussinessForBOM(DataTable pdtbComponent, int pintProductID)
        {
            try
            {
                ArrayList arrReturn = new ArrayList();
                ITM_HierarchyDS dsHierarchy = new ITM_HierarchyDS();
                DataTable dtbParent = dsHierarchy.ListParentOfProduct(pintProductID);
                dsHierarchy = null;

                //compare to find a wrong component
                for (int i = 0; i < pdtbComponent.Rows.Count; i++)
                {
                    if (pdtbComponent.Rows[i].RowState != DataRowState.Deleted)
                    {
                        foreach (DataRow drow in dtbParent.Rows)
                        {
                            if (pdtbComponent.Rows[i][ITM_BOMTable.COMPONENTID_FLD].ToString().Trim() == drow[0].ToString().Trim())
                            {
                                arrReturn.Add(pdtbComponent.Rows[i][ITM_BOMTable.COMPONENTID_FLD].ToString().Trim() + ";" + i.ToString());
                            }
                        }
                    }
                }
                return arrReturn;
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        //**************************************************************************              
        ///    <Description>
        ///		Create the query string to search pack list
        ///    </Description>
        ///    <Inputs>
        ///    </Inputs>
        ///    <Outputs>
        ///    </Outputs>
        ///    <Returns>
        ///       string
        ///    </Returns>
        ///    <Authors>
        ///       TuanDm
        ///    </Authors>
        ///    <History>
        ///       Tuesday, Mar 21, 2005
        ///    </History>
        ///    <Notes>
        ///    </Notes>
        //**************************************************************************
        //[AutoComplete(true)]
        public string ConditionToSearchItem(int pintCCNID, EnumAction enumAction)
        {
            string strCondition = " WHERE " + v_ITMBOM_Product.VIEW_NAME + "." + ITM_ProductTable.CCNID_FLD + " = " + pintCCNID.ToString() + " and " + v_ITMBOM_Product.VIEW_NAME + "." + ITM_ProductTable.MAKEITEM_FLD + " = 1 and "
                + v_ITMBOM_Product.VIEW_NAME + "." + v_ITMBOM_Product.HASBOM_FLD + " = ";
            if (enumAction == EnumAction.Add)
            {
                strCondition += "0";
            }
            else
            {
                strCondition += "1";
            }
            return strCondition;
        }

        //[AutoComplete(true)]
        public DataSet ListComponents()
        {
            ITM_BOMDS dsBOM = new ITM_BOMDS();
            return dsBOM.ListComponents();
        }
        //[AutoComplete(true)]
        public DataSet ListComponents(int pintProductID)
        {
            ITM_BOMDS dsBOM = new ITM_BOMDS();
            return dsBOM.ListComponents(pintProductID);
        }

        //[AutoComplete(true)]
        public DataRow GetItemInfo(int pintProductID)
        {
            ITM_ProductDS dsProduct = new ITM_ProductDS();
            return dsProduct.GetItemInfo(pintProductID);
        }

        //[AutoComplete(true)]
        public string GetWorkCenterInfo(int workCenterId)
        {
            MST_WorkCenterDS objMST_WorkCenterDS = new MST_WorkCenterDS();
            MST_WorkCenterVO voWorkCenter = (MST_WorkCenterVO)objMST_WorkCenterDS.GetObjectVO(workCenterId);
            if (voWorkCenter != null)
            {
                return voWorkCenter.Code;
            }

            return string.Empty;
        }
    }
}
